package com.wlw.eye.ui

import android.graphics.Typeface
import android.os.Bundle
import android.provider.Settings
import android.support.v4.app.DialogFragment
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.KeyEvent
import android.view.View
import android.widget.RadioGroup
import android.widget.Toast
import com.gyf.barlibrary.ImmersionBar
import com.wlw.eye.R
import com.wlw.eye.ui.fragment.*
import com.wlw.eye.utils.showToast
import kotlinx.android.synthetic.main.activity_main.*
import org.jetbrains.anko.find
import java.util.*
import kotlin.math.log
import kotlin.math.min

class MainActivity : AppCompatActivity(), RadioGroup.OnCheckedChangeListener {

    var toast: Toast? = null
    private var homeFragment: HomeFragment? = null
    private var findFragment: FindFragment? = null
    private var hotFragment: HotFragment? = null
    private var mineFragment: MineFragment? = null
    var mExitTime: Long = 0
    lateinit var searchFragment: SearchFragment
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        ImmersionBar.with(this).transparentBar().barAlpha(0.3f).fitsSystemWindows(true).init()
        val params = window.attributes
        params.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
        window.attributes = params
        initToolbar()
        setRadioButton()
        initFragment(savedInstanceState)
    }


    private fun initToolbar() {
        val today = getToday()
        tv_bar_title.text = today
        tv_bar_title.typeface = Typeface.createFromAsset(this.assets, "fonts/Lobster-1.4.otf")
        iv_search.setOnClickListener {
            if (rb_mine.isSelected) {

            } else {
                searchFragment = SearchFragment()
                searchFragment.show(supportFragmentManager, SEARCH_TAG)
            }
        }
    }

    private fun initFragment(savedInstanceState: Bundle?) {
        if (savedInstanceState != null) {
            val mFragments = supportFragmentManager.fragments
            for (item in mFragments) {
                if (item is HomeFragment)
                    homeFragment = item
            }
        } else {
            homeFragment = HomeFragment()
            findFragment = FindFragment()
            hotFragment = HotFragment()
            mineFragment = MineFragment()

            val fragmentTransaction = supportFragmentManager.beginTransaction()
            fragmentTransaction.add(R.id.fl_content, homeFragment)
            fragmentTransaction.add(R.id.fl_content, findFragment)
            fragmentTransaction.add(R.id.fl_content, hotFragment)
            fragmentTransaction.add(R.id.fl_content, mineFragment)
            fragmentTransaction.commit()
        }
        supportFragmentManager.beginTransaction().show(homeFragment)
                .hide(findFragment)
                .hide(hotFragment)
                .hide(mineFragment)
                .commit()
    }

    private fun getToday(): String {
        val list = arrayListOf("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
        val date = Date()
        val calendar = Calendar.getInstance()
        calendar.time = date
        var index: Int = calendar.get(Calendar.DAY_OF_WEEK) - 1
        if (index < 0)
            index = 0
        return list[index]
    }

    private fun setRadioButton() {
        rb_home.isChecked = true
        rg_root.setOnCheckedChangeListener(this)
    }

    override fun onCheckedChanged(group: RadioGroup?, checkedId: Int) {
        when (checkedId) {
            R.id.rb_find -> {
                tv_bar_title.text = "Discover"
                tv_bar_title.visibility = View.VISIBLE
                iv_search.setImageResource(R.mipmap.icon_search)
                supportFragmentManager.beginTransaction().show(findFragment)
                        .hide(homeFragment)
                        .hide(hotFragment)
                        .hide(mineFragment).commit()

            }
            R.id.rb_home -> {
                tv_bar_title.text = getToday()
                tv_bar_title.visibility = View.VISIBLE
                iv_search.setImageResource(R.mipmap.icon_search)
                supportFragmentManager.beginTransaction().show(homeFragment)
                        .hide(findFragment)
                        .hide(hotFragment)
                        .hide(mineFragment).commit()

            }
            R.id.rb_hot -> {
                tv_bar_title.text = "Ranking"
                tv_bar_title.visibility = View.VISIBLE
                iv_search.setImageResource(R.mipmap.icon_search)
                supportFragmentManager.beginTransaction().show(hotFragment)
                        .hide(homeFragment)
                        .hide(findFragment)
                        .hide(mineFragment)
                        .commit()
            }
            R.id.rb_mine -> {
                tv_bar_title.visibility = View.INVISIBLE
                iv_search.setImageResource(R.mipmap.icon_setting)
                supportFragmentManager.beginTransaction().show(mineFragment)
                        .hide(homeFragment)
                        .hide(findFragment)
                        .hide(hotFragment).commit()
            }
        }
    }

    override fun onPause() {
        super.onPause()
        toast?.let {
            toast!!.cancel()
        }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (System.currentTimeMillis().minus(mExitTime) <= 3000) {
                toast!!.cancel()
                finish()
            } else {
                mExitTime = System.currentTimeMillis()
                toast = showToast("再按一次退出")
            }
            return true
        }
        return super.onKeyDown(keyCode, event)
    }
}
